package service

import (
	__ "auth-server/basic/proto"
	"auth-server/handler/model"
	"context"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedAuthServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Auth(_ context.Context, in *__.AuthReq) (*__.AuthResp, error) {

	auth := model.Auth{}
	//查询用户是否存在
	if err := auth.FindUserBy(); err != nil {
		return &__.AuthResp{
			Code:    400,
			Message: "用户查询失败",
		}, nil
	}

	//判断用户是否存在
	if auth.ID == 0 {
		return &__.AuthResp{
			Code:    400,
			Message: "用户不存在",
		}, nil
	}

	//判断密码是否正确
	if auth.Password != in.Password {
		return &__.AuthResp{
			Code:    400,
			Message: "密码错误",
		}, nil
	}

	//登录成功返回
	return &__.AuthResp{
		Code:    200,
		Message: "登录成功",
	}, nil
}
